<?php
/**
 * 主题类
 */

include_once FUN_PTH.'theme.inc.php';

class Theme extends MyBase{
	
	function __construct() {
		parent::__construct('theme.id', array('keyword' => array('name', 'desc')));
	}
	
	/**
	 * 修改主题
	 */
	public function edit($post){
		$id = empty($post['id']) ? 0 : intval($post['id']);
		if($id <= 0)return false;
		$select = array('name' => $post['name'], 'kept' => " AND id!={$id}");
		if($id > 0){
			$select['kept'] = " AND id!={$id}";
		}
		$rs = $this->db->get_row($this->tbname, $select, 'id');
		if(!empty($rs)){return lang('already').lang('exist');}
		$data = array(
			'name'		=> $post['name'],
			'url'		=> $post['url'],
			'author'	=> $post['author'],
			'version'	=> $post['version'],
			'pubdate'	=> $post['pubdate'],
			'desc'		=> $post['desc'],
		);
		if($id > 0){
			$data['updatetime'] = TIME;
			$r = $this->db->update($this->tbname, array('id' => $id), $data);
		}else{
			$data['addtime'] = TIME;
			$r = $id = $this->db->insert($this->tbname, $data);
		}
		if($r > 0){
			return $this->cache_theme(); // 缓存主题
		}
		return $r;
	}
	
	public function update(){
		$themes = get_themes();
		foreach($themes as $mark => $theme){
			$data = array (
				'mark'		=> $theme['mark'],
				'name'		=> $theme['name'],
				'url'		=> $theme['url'],
				'author'	=> $theme['author'],
				'version'	=> $theme['version'],
				'pubdate'	=> $theme['pubdate'],
				'desc'		=> $theme['desc'],
				'preview'	=> json_encode($theme['preview']),
			);
			$rs = $this->get_row_by_mark($mark, '`id`');
			if(!empty($rs)){
				$data['updatetime'] = TIME;
				// $r = $this->db->update($this->tbname, array('id' => $rs['id']), $data);
			}else{
				$data['addtime'] = TIME;
				$r = $id = $this->db->insert($this->tbname, $data);
			}
		}
		return $this->cache_theme(); // 缓存主题
	}
		
	/**
	 * 更新排序
	 */
	public function rank($post){
		if(!is_array($post['rank']))$post['rank'] = array($post['rank']);
		$iRtn = 0;
		foreach($post['lst'] as $id){
			$data = array(
				'rank'		 => $post['rank'][$id], 
				'updatetime' => TIME 
			);
			if($this->db->update($this->tbname, array('id' => $id), $data)){
				$iRtn ++;
			}
		}
		return $this->cache_theme();
	}
	
	public function activate($post){
		$post['list'] = empty($post['list']) ? array() : $post['list'];
		$iRtn = 0;
		foreach($post['list'] as $id){
			$theme = $this->get_row($id);
			$result = activate_theme($theme['mark']);
			if($result){	// 处理成功
				$data = array(
					'state'		 => 1, 
					'seek'		 => 1, 
					'updatetime' => TIME,
				);
				$this->db->update($this->tbname, array('id' => $id), $data);
				$this->db->update($this->tbname, array('kept' => " AND `id`!={$id}"), array('state' => 0));
			}
			
			$r = $this->cache_theme($theme['mark']);
			if($r){
				$iRtn ++;
			}
		}
		return $iRtn;
	}
			
	
	/**
	 * 取一条记录
	 */
	/*public function get_row($id, $fields = '*'){
		return $this->db->get_row($this->tbname, array('id' => $id), $fields);
	}*/
	
	public function get_row_by_mark($mark, $fields = '*'){
		return $this->db->get_row($this->tbname, array('mark' => $mark), $fields);
	}
	
	/**
	 * 取列表
	 */
	/*public function get_list($fields = '*', $select = array(), $order = array(), $page = 1, $size = 10, $offset = PAGE_OFFSET){
		$select['kept'] = '';
		if(!empty($select['keyword'])){
			$select['kept'] .= " AND (`name` LIKE '%{$select['keyword']}%' OR `desc` LIKE '%{$select['keyword']}%')";
		}
		return $this->db->get_list($this->tbname, $fields, $select, $order, $page, $size, $offset);
	}*/
	
	/*public function get_all($fields = '*', $select = array(), $order = array()){
		$select['kept'] = '';
		if(!empty($select['keyword'])){
			$select['kept'] .= " AND (`name` LIKE '%{$select['keyword']}%' OR `desc` LIKE '%{$select['keyword']}%')";
		}
		return $this->db->get_all($this->tbname, $fields, $select, $order);
	}*/
	
	/********************************************************************************
	 * 缓存
	 ********************************************************************************/
		
	public function cache_theme($mark = ''){
		$list = $this->get_all('*', array(), array('rank' => 'DESC', 'id' => 'ASC'));
		if(!empty($list['list'])){
			$themes = array();
			foreach($list['list'] as $theme){
				$theme['preview'] = json_decode($theme['preview']);
				$themes[$theme['mark']] = $theme;
			}
			if(!empty($mark)){
				$theme = $themes[$mark];
				cache_write('theme.php', $theme);
			}
			cache_write('theme_all.php', $themes);
		}
		return true;
	}
	
}
?>